package com.heyi.nettylong.client.handler;

import com.heyi.nettylong.common.MessageHandler;
import com.heyi.nettylong.common.protobuf.Command;
import com.heyi.nettylong.common.protobuf.Message;
import com.heyi.nettylong.common.util.MessageUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;

/**
 * @author dengfeng
 * @date 2022/12/717:38
 */
public class ClientMessageHandler extends MessageHandler {
    String token;

    public ClientMessageHandler(ChannelHandlerContext ctx, Message.MessageBase msgBase, String token) {
        super(ctx, msgBase);
        this.token = token;
    }

    @Override
    public ChannelFuture sendMessage(String jsonString){
        ChannelFuture cf = ctx.writeAndFlush(MessageUtil.build(msgBase.getMsgId(), msgBase.getClientId(), msgBase.getMsgId()>0 ? Command.CommandType.PUSH_DATA_BACK : Command.CommandType.PUSH_DATA, jsonString, token));
        if(msgBase.getMsgId() > 0) {
            msgBase = MessageUtil.build(msgBase.getClientId(), Command.CommandType.PUSH_DATA, msgBase.getData(), token);
        }
        return cf;
    }
}
